﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Configuration;

namespace Hands.Unit.Web
{
    internal class SmtpProxy : ISendMail
    {
        private MailMessage message = new MailMessage();
        private SmtpClient proxy = new SmtpClient();
        private string password = null;

        private bool isAddFrom = false;
        private bool isAddMessage = false;

        private static string defaultFrom = null;
        private static string defaultFromPassword = null;

        static SmtpProxy()
        {
            defaultFrom = ConfigurationManager.AppSettings["from"];
            defaultFromPassword = Encoding.GetEncoding("UTF-8").GetString(Convert.FromBase64String(ConfigurationManager.AppSettings["password"]));
        }

        #region ISendMail 成员

        public ISendMail addTo(string to)
        {
            message.To.Add(to);
            return this;
        }

        public ISendMail addTo(List<string> toList)
        {
            foreach (string to in toList)
            {
                message.To.Add(to);
            }
            return this;
        }

        public ISendMail addMessage(string title, string content)
        {
            if (this.isAddMessage) return this;
            addMessageContent(title, content);
            message.IsBodyHtml = true;
            this.isAddMessage = true;
            return this;
        }

        public ISendMail addMessage(string title, string content, bool isBodyHtml)
        {
            if (this.isAddMessage) return this;
            addMessageContent(title, content);
            message.IsBodyHtml = isBodyHtml;
            this.isAddMessage = true;
            return this;
        }

        private void addMessageContent(string title, string content)
        {
            message.Subject = title;
            message.SubjectEncoding = Encoding.UTF8;
            message.Body = content;
            message.BodyEncoding = Encoding.UTF8;
        }

        public ISendMail addFrom(string from, string senderPassword)
        {
            if (this.isAddFrom) return this;
            message.From = new MailAddress(from);
            this.password = senderPassword;
            this.isAddFrom = true;
            return this;
        }

        public ISendMail addAttachments(string path)
        {
            throw new NotImplementedException();
        }

        public ISendMail addAttachments(List<string> paths)
        {
            throw new NotImplementedException();
        }

        public void send()
        {
            setMailSendPara();
            proxy.Send(message);
        }

        public void sendAsync(SendCompletedEventHandler onCompleted)
        {
            setMailSendPara();
            proxy.SendCompleted += onCompleted;
            proxy.SendAsync(message, message.Body);
        }

        private void setMailSendPara()
        {
            if (isAddFrom)
            {
                proxy.Credentials = new NetworkCredential(message.From.Address, this.password);
            }
            else
            {
                proxy.Credentials = new NetworkCredential(defaultFrom, defaultFromPassword);
            }
            proxy.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            proxy.Host = "smtp." + message.From.Host;
        }
        #endregion
    }
}
